//
//  MileageRingView.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/3.
//

#import "MileageRingView.h"
#import "Public.h"

@interface MileageRingView ()

@property (nonatomic, strong) UILabel *todayLbl;
@property (nonatomic, strong) UILabel *todayTitleLbl;

@end

@implementation MileageRingView

- (void)setCurrentDistance:(CGFloat)currentDistance allDistance:(CGFloat)allDistance comple:(NSUInteger)comple {
    self.currentDistance = currentDistance;
    self.comple = comple;
    self.allDistance = allDistance;
    [self setNeedsDisplay];
}

- (instancetype)init
{
    if(self = [super init]) {
        self.todayLbl = [UILabel new];
        self.todayLbl.textColor = rgbHex(0x0AD39F, 1.0);
        self.todayLbl.textAlignment = NSTextAlignmentCenter;
        self.todayLbl.adjustsFontSizeToFitWidth = YES;
        self.todayLbl.minimumScaleFactor = 0.6;
        self.todayLbl.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.todayLbl];
        
        self.todayTitleLbl = [UILabel new];
        self.todayTitleLbl.textColor = rgbHex(0x0AD39F, 1.0);
        self.todayTitleLbl.textAlignment = NSTextAlignmentCenter;
        self.todayTitleLbl.adjustsFontSizeToFitWidth = YES;
        self.todayTitleLbl.minimumScaleFactor = 0.6;
        self.todayTitleLbl.text = CycLocalizedString(@"TODAY", "今天");
        self.todayTitleLbl.font = PingFangFont(10);
        [self addSubview:self.todayTitleLbl];
    }
    return self;
}

- (void)reloadDistanceValueWithRadius:(CGFloat)radium
{
    NSString *distanceString = [NSString stringWithFormat:@"%.1f", self.currentDistance / 1609.344];
    NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:distanceString attributes:@{NSFontAttributeName : PingFangSemiboldFont(16)}];
    [attributes appendAttributedString:[[NSAttributedString alloc] initWithString:@" miles" attributes:@{NSFontAttributeName : PingFangFont(10)}]];
    self.todayLbl.attributedText = attributes;
    
//    self.todayLbl.text = [NSString stringWithFormat:@"%@mile", distanceString];
    
    if(self.comple == 2) {
        self.todayLbl.textColor = rgbHex(0xfa2c1e, 1.0);
    } else {
        if(self.currentDistance == self.allDistance) {
            self.todayLbl.textColor = rgbHex(0x08dcaa, 1.0);
        } else {
            self.todayLbl.textColor = rgbHex(0x00e0a7,1.0);
        }
    }
    self.todayTitleLbl.textColor = self.todayLbl.textColor;
    
    CGFloat topOffset = 17;
    self.todayLbl.ce_height = self.todayLbl.intrinsicContentSize.height;
    self.todayLbl.ce_width = 2 * sqrtf(pow(radium, 2) - pow(topOffset, 2)) - 4;
    self.todayLbl.ce_y = self.ce_height / 2 - topOffset;
    self.todayLbl.ce_centerX = self.ce_width / 2;
    
    topOffset = 19;
    self.todayTitleLbl.ce_height = self.todayTitleLbl.intrinsicContentSize.height;
    self.todayTitleLbl.ce_width = 2 * sqrt(pow(radium, 2) - pow(topOffset, 2)) - 4;
    self.todayTitleLbl.bottom = self.ce_height / 2 + topOffset;
    self.todayTitleLbl.ce_centerX = self.ce_width / 2;
}

- (void)drawRect:(CGRect)rect {
    self.backgroundColor =[UIColor whiteColor];
    
    // 背景圆环
    UIBezierPath *bgpath = [[UIBezierPath alloc] init];
    bgpath.lineWidth = 10;
    [COLOR_BGRED set];
    bgpath.lineCapStyle = kCGLineCapRound;
    bgpath.lineJoinStyle = kCGLineJoinRound;
    CGFloat radius = (MIN(rect.size.width, rect.size.height) - 10)/2;
    [bgpath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    [bgpath stroke];

    if(self.comple == 2) { // 未完成
        
        UIBezierPath *path = [[UIBezierPath alloc] init];
        path.lineWidth = 10;
        UIColor *color = rgbHex(0xfa2c1e, 1.0);
        [color set];
        path.lineCapStyle = kCGLineCapRound;
        path.lineJoinStyle = kCGLineJoinRound;
        CGFloat radius = (MIN(rect.size.width, rect.size.height) - 10)/2;
        [path addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) radius:radius startAngle:0 endAngle:-(self.currentDistance/self.allDistance)*2*M_PI clockwise:NO];
        [path stroke];

    }else { // 完成
        if(self.currentDistance == self.allDistance){
            UIBezierPath *path = [[UIBezierPath alloc] init];
            path.lineWidth = 10;
            UIColor *color = rgbHex(0x08dcaa, 1.0);
            [color set];
            path.lineCapStyle = kCGLineCapRound;
            path.lineJoinStyle = kCGLineJoinRound;
            CGFloat radius = (MIN(rect.size.width, rect.size.height) - 10)/2;
            [path addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) radius:radius startAngle:0 endAngle:-(self.currentDistance/self.allDistance)*2*M_PI clockwise:NO];
            [path stroke];
        }else {
            // 1. 还是添加一个圆弧路径
            // 获取当前context
            CGContextRef ctx = UIGraphicsGetCurrentContext();
            // 设置线的宽度
            CGContextSetLineWidth(ctx, 10);
            // 设置线条端点为圆角
            CGContextSetLineCap(ctx, kCGLineCapRound);
            // 设置画笔颜色
            CGContextSetFillColorWithColor(ctx, COLOR_DISABLEED.CGColor);
            CGFloat originX = rect.size.width / 2;
            CGFloat originY = rect.size.height / 2;
            // 计算半径
            CGFloat radius = MIN(originX, originY) - 10/2.0;
            // 逆时针画一个圆弧
            CGContextAddArc(ctx, rect.size.width / 2, rect.size.height / 2, radius, 0, -M_PI*2, 1);
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

            NSArray *colorArr = @[
                    (id)rgbHex(0x00e0a7,1.0).CGColor,
                    (id)rgbHex(0x00bdff,1.0).CGColor
                ];

            CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colorArr, NULL);
            CGColorSpaceRelease(colorSpace);
            colorSpace = NULL;

            CGContextReplacePathWithStrokedPath(ctx);
            CGContextClip(ctx);
            CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, rect.size.height), CGPointMake(rect.size.width, 0), 1);
            CGGradientRelease(gradient);
        }
    }
    [self reloadDistanceValueWithRadius:radius];
}

@end
